﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace AndroidTools
{
    public partial class FrmFile : Form
    {
        public FrmIDA frmIDA;
        List<File> files;

        public FrmFile()
        {
            InitializeComponent();
        }



        private void FrmFile_Load(object sender, EventArgs e)
        {
            string path = string.Format("/data/local/tmp");
            txtFile.Text = path;
            refreshFiles();
        }

        private void refreshFiles()
        {
            if(txtFile.Text == "")
            {
                txtFile.Text = "/";
            }

            listBox1.Items.Clear();
            files = ApkHelper.GetPathList(Global.devices[Global.selectDeviceIdx].Name, txtFile.Text);
            foreach (File file in files)
            {
                listBox1.Items.Add(file.Name);
            }
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == -1) return;
            File file = files[listBox1.SelectedIndex];
            txtFile.Text = file.Path;
        }

        private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            File file = files[listBox1.SelectedIndex];
            if (file.IsDir)
            {
                txtFile.Text = file.Path;
                refreshFiles();
            }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if(txtFile.Text == "/")
            {
                return;
            }

            txtFile.Text = txtFile.Text.Substring(0, txtFile.Text.LastIndexOf("/")) ;
            refreshFiles();
        }

        private void btnFile_Click(object sender, EventArgs e)
        {
            File file = files[listBox1.SelectedIndex];
            if (!file.IsDir)
            {
                Global.androidServerPath = file.Path;
                frmIDA.SettingTxtAndSerPath();
                this.Close();
            }
            else
            {
                txtFile.Text = file.Path;
                refreshFiles();
            }
        }
    }
}
